package cn.iocoder.yudao.module.reading.controller.app.community;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.common.LikeToggleRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.post.PostCreateReqVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.post.PostPageReqVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.post.PostRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.post.PostUpdateReqVO;
import cn.iocoder.yudao.module.reading.service.community.CommunityPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 社区帖子 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 社区帖子")
@RestController
@RequestMapping("/reading/community/posts")
@Validated
@Slf4j
public class CommunityPostController {

    @Resource
    private CommunityPostService postService;

    @PostMapping("/create")
    @Operation(summary = "创建帖子")
    @PreAuthenticated
    public CommonResult<Long> createPost(@Valid @RequestBody PostCreateReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        Long postId = postService.createPost(createReqVO, userId);
        return success(postId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新帖子")
    @PreAuthenticated
    public CommonResult<Boolean> updatePost(@Valid @RequestBody PostUpdateReqVO updateReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        postService.updatePost(updateReqVO, userId);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除帖子")
    @PreAuthenticated
    public CommonResult<Boolean> deletePost(@RequestParam("id") Long id) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        postService.deletePost(id, userId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子详情")
    public CommonResult<PostRespVO> getPost(@RequestParam("id") Long id) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        PostRespVO post = postService.getPost(id, userId);
        return success(post);
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子分页")
    public CommonResult<PageResult<PostRespVO>> getPostPage(@Valid PostPageReqVO pageReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        PageResult<PostRespVO> pageResult = postService.getPostPage(pageReqVO, userId);
        return success(pageResult);
    }

    @PostMapping("/{postId}/like")
    @Operation(summary = "切换帖子点赞状态")
    @PreAuthenticated
    public CommonResult<LikeToggleRespVO> togglePostLike(
            @Parameter(description = "帖子ID", required = true) @PathVariable("postId") Long postId) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        LikeToggleRespVO result = postService.togglePostLike(postId, userId);
        return success(result);
    }

}
